package pub.tbc.api.config;

import lombok.extern.slf4j.Slf4j;
import pub.tbc.toolkit.core.EmptyUtil;
import pub.tbc.toolkit.core.Props;
import pub.tbc.toolkit.core.collect.Maps;

import java.util.Map;
import java.util.Properties;

/**
 * @author tbc on 2017/3/24 12:16:15.
 */
@Slf4j
public class ConfigProvider {
    private static String DEFAULT_CONFIG_FILE = "application";
    private static String DEFAULT_POSTFIX = ".properties";
    private static Map<String, PropertiesConfig> props = Maps.newConcurrentHashMap();

    public static Config getPropertiesConfig() {
        return getPropertiesConfig(DEFAULT_CONFIG_FILE);
    }

    public static Config getPropertiesConfig(String file) {
        // 先检查缓存里有没有
        PropertiesConfig p = props.get(file);
        if (EmptyUtil.isEmpty(p)) {
            Properties prop = Props.getInstance().load(file + DEFAULT_POSTFIX);
            p = new PropertiesConfig(prop);
            props.put(file, p);
        }
        return p;
    }

}
